<?php
/**
 * 文章中心
 * power by huanran.zhao
 * QQ: 523994553
 * 2013-04-09
 */
class ListingApp extends CommonApp {
	
	private $table = 'datas';

	/**
	 * 首页
	 */
	public function indexAct(){
		$item = My::arr2var($_REQUEST,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,'文章分类有误!');
		//search
		$search = array();
		$search['where'] = "1 ";
		$search['where'] .= "AND A.`item`='{$item}' ";
		//publish
		if(My::arr2var($_GET,'publish')==='0'){
			$search['where'] .= "AND A.publish=0 ";
		}elseif(My::arr2var($_GET,'publish')==1){
			$search['where'] .= "AND A.publish=1 ";
		}
		$search['field'] = "A.*";
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
		$search['table'] = "#__{$this->table} as A";
		$search['join']  = "LEFT JOIN #__{$this->table}_params as B ON A.id=B.idid ";
		//搜索产品分类
		foreach($_GET as $key=>$value){
			if(substr($key,0,7)=='params.'){
				$_k = substr($key,7);
				$search['join'] .= "INNER JOIN #__{$this->table}_params as {$_k} ON {$_k}.`value`='{$value}' AND {$_k}.`key`='{$_k}' AND A.id={$_k}.idid ";
			}
		}
		//搜索标题
		if(My::arr2var($_GET,'title')!=''){
			$title = I('get.title');
			$search['where'] .= "AND A.`title` like '%{$title}%' ";
		}
		//搜索标题
		if(My::is_id(My::arr2var($_GET,'id'))){
			$id = $_GET['id'];
			$search['where'] .= "AND A.`id`='{$id}' ";
		}
		$result=My::getCache($search,array('pre'=>$this->table));
		if($result==''){
			//page
			$total_sql = DB('')->table($search['table'])->field('1')->join($search['join'])->where($search['where'])->group('A.id')->select(false);
			$result['page'] = My::page(My::sql2count($total_sql));
			//result
			$result['data'] = DB('')->table($search['table'])->field($search['field'])->join($search['join'])->where($search['where'])->group('A.id')->order('A.ordering ASC,A.adddate DESC,A.id DESC')->limit($result['page']['limits'])->select();
			//echo DB('')->getLastSql();
			if(is_array($result['data']))
			foreach($result['data'] as &$value){
				$value['params'] = My::decodeJson($value['params']);
			}
			My::setCache($search,$result,array('pre'=>$this->table));
		}
		$this->assign('result',$result);
		//print_r($result);
		//display
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/index");
	}
	
	/**
	 * 页面显示(编辑/添加)
	 * @params $_GET['id']
	 */
	public function modifyAct(){
		//分类唯一名称
		$item = My::arr2var($_GET,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,"分类错误!");
		//id
		$id = I('get.id',0);
		$result = array();
		if(My::is_id($id)){
			//当前id文章数据
			$result = DB($this->table)->where("id={$id}")->find();
			$result['content'] = My::decodeHtml($result['content']);
			$result['params'] = My::decodeJson($result['params']);
			$this->assign('result',$result);
		}
		$goods_type = M('category')->item2level('goods_type',array('level'=>'1'));//商品类型
		$this->assign('goods_type',$goods_type);
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		$this->assign('item',$item);
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/modify");
	}
	
	/**
	 * 保存(编辑/添加)
	 * @param string $_POST['URL'] 当前页面url
	 * @param string $_POST['URL_'] 上一页面url
	 * @param int $_POST['id']  当前操作id(可选)
	 * @param $_POST post表单数据
	 */
	public function saveAct(){
		$item = My::arr2var($_POST,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(300,'item有误!');
		$this->verify($item);
		if(!token())
			$this->ajaxReturn(300,'令牌验证错误!');
		if(My::arr2var($_POST,'title')=='')
			$this->ajaxReturn(300,"标题不能为空!");
		$id = $_POST['id'] = My::arr2var($_POST,'id');
		$params = My::arr2var($_POST,'params');
		if(My::is_id($id)){//编辑
			/*//白名单
			if($item=='activity')
				$params = My::keepParams($params,array('table'=>$this->table,'id'=>$id,'verifyId'=>'activity'));*/
			//保存表单模型参数
			My::setParams($this->table,$params,$id,$item);
			//dump($_POST);exit;
			$ok = DB($this->table)->clear()->save($_POST);
			if($ok)	$this->ajaxReturn(200,"编辑成功");
			else		$this->ajaxReturn(300,"编辑失败");
		}else{//添加
			$_POST['adddate'] = date("Y-m-d H:i:s");
			$_POST['author'] = My::user('username');
			$ok = DB($this->table)->clear()->add($_POST);
			//保存表单模型参数
			My::setParams($this->table,$params,$ok,$item);
			if($ok>=0)	$this->ajaxReturn(200,"添加成功");
			else		$this->ajaxReturn(300,"添加失败");
		}
	}

}